package com.my.ycv51.util;

public class Change {
    private static final String TAG = "Change";
    //十六进制字符串转十六进制字节码
    public  static   byte[] toBytes(String str) {
        try {
            if (str == null || str.trim().equals("")) {
                return new byte[0];
            }
            byte[] bytes = new byte[str.length() / 2];
            for (int i = 0; i < str.length() / 2; i++) {
                String subStr = str.substring(i * 2, i * 2 + 2);
                bytes[i] = (byte) Integer.parseInt(subStr, 16);
            }
            return bytes;
        }catch (Exception ex){
//            LocalLogUtil.WriteLog(TAG, LocalLogType.ERROR, ex.toString());
            return new byte[0];
        }
    }

    //十六进制字节码转十六进制字符串
    public static String bytesToHex(byte[] bytes) {
        try {
            StringBuilder buf = new StringBuilder(bytes.length * 2);
            for (byte b : bytes) { // 使用String的format方法进行转换
                buf.append(String.format("%02x", new Integer(b & 0xff)));
            }
            return buf.toString().toUpperCase();
        }catch (Exception ex){
//            LocalLogUtil.WriteLog(TAG, LocalLogType.ERROR, ex.toString());
            return "";
        }

    }

}
